<?php
namespace App\Repositories;

use App\Models\Banner;
use App\Exceptions\InvalidException;
use App\Identity\Repo;
use Log;
use Illuminate\Database\Eloquent\Model;
class BaseRepo  implements Repo
{
	public static $model = null;

    public function getInstance(){
        if(self::$model instanceof Model) {
            return self::$model;
        }  else {
            throw new InvalidException("未获取数据链接资源");
        }
    }
    public function getAll()
    {
        return $this->getInstance()::all();
    }
    public function getOneById($id)
    {
        return self::$model::where('id',$id)->first();
    }
    public function create($data)
    {
          try{
            $this->getInstance()::create($data);
        }catch(\Exception $e){
            Log::info($e->getMessage());
            throw new InvalidException("创建失败");
        }
    }
    public function update(int $id, array $data)
    {
        $obj = $this->getInstance()::where('id',$id)->first();
        if(!$obj)  {
            throw new InvalidException("更新失败1");
        }
        try{
            $obj::where('id',$id)->update($data);
        }catch(\Exception $e){
            Log::info($e->getMessage());
            throw new InvalidException("更新失败2");
        }

    }
    public function del($id)
    {
        if(!$id) {
            throw new InvalidException('缺少参数ID');
        }
        try {
            $this->getInstance()::where('id',$id)->delete();
        } catch(\Exception $e) {
            Log::info($e->getMessage);
            throw new InvalidException("无法删除");
        }
    }
    public function call_static()
    {
        
    }
}
